from selenium.webdriver.common.alert import Alert
from selenium.webdriver.common.by import By
import unittest

from quote.util.logInfo import LogInfo


class BrowserOP:

    def __init__(self,driver):
        self.driver = driver
        self.driver.implicitly_wait(5)
        self.li = LogInfo()


    def open_url(self,url):  #打开网页
        self.li.set_message('info','打开浏览器:'+url)
        self.driver.get(url)

    def find_element(self,type,locator):   #返回元素
        try:
            element = None
            if type =='id':
                element = self.driver.find_element(By.ID,locator)
            elif type =='name':
                element = self.driver.find_element(By.NAME,locator)
            elif type =='xpath':
                element = self.driver.find_element(By.XPATH,locator)
            return element
        except Exception as e:
            self.li.set_message('error',e)

    def input_text(self,type,locator,text):  #输入文本操作
        try:
             self.li.set_message('info','输入文本:'+'locatar:'+locator+'   '+text)
             self.find_element(type,locator).send_keys(text)
        except Exception as e:
            self.li.set_message('error',e)

    def click(self,type,locator):  #点击操作
        try:
             self.li.set_message('info','点击操作:'+'locator:'+locator)
             self.find_element(type,locator).click()
        except Exception as e:
            self.li.set_message('error',e)


    def get_text(self,type,locator): #获取文本
        try:
            return self.find_element(type,locator).text
        except Exception as e:
            self.li.set_message('error',e)

    def change_frame(self,frame_name):   #切换框架
        self.driver.switch_to.parent_frame()
        self.driver.switch_to.frame(frame_name)

    # 切换window功能
    def change_window(self, title):
        for window_id in self.driver.window_handles:
            self.driver.switch_to.window(window_id)
            if self.driver.title == title:
                break

    def alter_op(self,op):  #弹出框操作
        alter = Alert(self.driver)
        if op=='1':
            alter.accept()
        elif op=='0':
            alter.dismiss()




    def show_all(self):  #遍历
        v = self.get_text('xpath','/html/body/center/form/table[2]/tbody/tr/td/font')
        for i in range(1,v[34]+1):




            self.click('LINK_TEXT',"第"+str(i)+"页")







# if __name__ == '__main__':
#     ub = UseBrowser()
#     a = BrowserOP(UseBrowser.driver)
#     a.open_url('http://localhost:8080/JavaPrj_6/')
#     a.input_text('name','username','admin')
#     a.input_text('name','password','admin')
#     a.click('name','submit')